function ArrayList() {
    this.array = []

    ArrayList.prototype.swap = (m, n) => {
        let temp = this.array[m]
        this.array[m] = this.array[n]
        this.array[n] = temp
    }

    ArrayList.prototype.insert = item => {
        this.array.push(item)
    }

    ArrayList.prototype.toString = () => {
        return this.array.join('-')
    }

    ArrayList.prototype.insertSort = () => {//插入排序
        //1.获取数组长度
        let length = this.array.length;

        //2.外层循环：从第一个位置获取元素，向前面局部有序进行插入
        for (let i = 1; i < length; i++) {
            var temp = this.array[i];
            //3.因为在内层while循环中要对i操作，所以用j来代替i
            var j = i;
            //4.内层循环：获取i位置的元素和前面的数据进行比较
            while (this.array[j - 1] > temp && j > 0) {
                this.array[j] = this.array[j - 1];

                j--;//通过减小j值，达到多次比较
            }
            //5.将j位置的数据放置temp
            this.array[j] = temp;
        }
    }

}

let list = new ArrayList();
list.insert(2);
list.insert(5);
list.insert(1);
list.insert(4);
list.insert(3);
list.insert(13);

console.log(list.toString());
list.insertSort();
console.log(list.toString());

